package offer32三;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution1 {
    public List<List<Integer>> levelOrder(TreeNode root) {

        List<List<Integer>> ret = new ArrayList<>();
        if(root == null) return ret;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            LinkedList<Integer> level = new LinkedList<>();
            for(int i = 0 ; i < queue.size();i++){
                TreeNode tmp = queue.poll();
                if(ret.size()%2==0){
                    level.addLast(tmp.val);
                }else{
                    level.addFirst(tmp.val);
                }
                if(tmp.left!=null) queue.add(tmp.left);
                if(tmp.right!=null) queue.add(tmp.right);
            }
        }
        return ret;
    }
}
